package MapReduce.Demo11_ProductSerch;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import utils.JobSubmit;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author lixinlei
 * @Date 2023/3/22 10:34
 */
public class ProductSearchCountApp {

    public static class PSCMapper extends Mapper<LongWritable, Text,Text,Text>{

        Text outKey = new Text();
        Text outValue = new Text();

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {

            //从上下文中获取当前的输入切片，向下转型为 FileSplit 目的是为了使用 获取路径的方法
            FileSplit inputSplit = (FileSplit) context.getInputSplit();

            //从输入切片中获取文件名
            String fileName = inputSplit.getPath().getName();
            outValue.set(fileName);

            //获取所有的商品名
            String line = value.toString();
            String[] products = line.split(" ");

            //遍历商品名输出
            for (String product : products) {
                outKey.set(product);
                context.write(outKey,outValue);
            }

        }
    }

    public static class PSCReducer extends Reducer<Text,Text,Text,Text>{

        Text outValue = new Text();

        /**
         * 数据格式
         *      手机 Iterable(pdd.txt pdd.txt jd.txt ......)
         * @param key
         * @param values
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {

            Map<String,Integer> fileNameMap = new HashMap<>();

            //把当前商品的所有平台加入到 Map 中，使用 value进行计数
            for (Text value : values) {
                String fileName = value.toString();
                //如果Map中存在这个文件名，把原计数值读出 加1后更新
                if(fileNameMap.containsKey(fileName)){
                    fileNameMap.put(fileName,fileNameMap.get(fileName)+1);
                }else{
                    fileNameMap.put(fileName,1);
                }
            }


            StringBuffer sb = new StringBuffer();
            //把当前商品的所有平台和计数值进行遍历拼接
            Set<Map.Entry<String, Integer>> entries = fileNameMap.entrySet();
            for (Map.Entry<String, Integer> entry : entries) {
                sb.append(entry.getKey()+":"+entry.getValue()+",");
            }

            String platform = sb.toString();
            platform = platform.substring(0,platform.lastIndexOf(","));
            platform = platform.replace(".txt","");

            outValue.set(platform);
            context.write(key,outValue);
        }
    }

    public static void main(String[] args) {

        JobSubmit.submitBaseJob(
                ProductSearchCountApp.class,
                args
        );

    }
}
